/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMTermClassSettingsDialog;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import GuiTools.*;
import Settings.*;
import Storage.TermClass;

/**
 * @author Christoph Schnack
 *
 */
class ATCCDTermClassPanel extends JPanel{
	
	private TermClass oldTC;
	private String oldTCName;
	private JPanel newPreFixPanel,newMetaInformationPanel;
	private MetaElement newPreFixME;
	private JComboBox tcJCB;
	private ConvertPrefixPanel prefixConvertPanel;
	private Vector<ConvertMetaInformationPanel> oldMIPanels;
	private Vector<MetaElement> newMIPanels;
	private HashMap<String, TermClass> oldTermClasses,newTermClasses;
	private Vector<String> newSortedKeys;
	private MDTribuitFrame frame;
	private Language LANG;
	private Settings SET;
	
	public ATCCDTermClassPanel(MDTribuitFrame parent,Settings set,String oldTCName_Arg,TermClass oldTC_Arg, HashMap<String,TermClass> oldTCs, HashMap<String,TermClass> nTCs, Vector<String> NSK){
		super();
		oldTCName = oldTCName_Arg;
		oldTC = oldTC_Arg;
		oldTermClasses = oldTCs;
		newTermClasses = nTCs;
		newSortedKeys = NSK;
		frame = parent;
		SET = set;
		LANG = SET.getLanguage();
		JLabel title = new JLabel(oldTCName);
		title.setFont(new Font(Font.SANS_SERIF,Font.BOLD,18));
		
		tcJCB = new JComboBox(newSortedKeys);
		tcJCB.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e){
				setNewTCPanel();
				frame.pack();
				frame.repaint();
			}
		});
		
		oldMIPanels = new Vector<ConvertMetaInformationPanel>();
		prefixConvertPanel = new ConvertPrefixPanel(oldTC.getPreFixName(),oldTC.getPreFixVal());
		JPanel oldPrefixPanel = new JPanel();
		oldPrefixPanel.setBorder(BorderFactory.createTitledBorder(LANG.getArticle()+(oldTC.hasPrefix()?" ("+oldTC.getPreFixName()+", "+(oldTC.getPreFixVal()==null?"text field":"combo box")+")":"")));
		if(oldTC.hasPrefix()){
			oldPrefixPanel.add(prefixConvertPanel);
		}
		JPanel oldMIPanel = new JPanel();
		oldMIPanel.setLayout(new GridBagLayout());
		GridBagConstraints omiGBC = new GridBagConstraints();
		omiGBC.fill = GridBagConstraints.BOTH;
		omiGBC.anchor = GridBagConstraints.NORTHWEST;
		omiGBC.insets = new Insets(10,10,0,10);
		if(!oldTC.isEmpty()){
			for(String s : oldTC.getSortedKeys()){
				omiGBC.gridy++; 
				oldMIPanels.add(new ConvertMetaInformationPanel(s,oldTC.getValues(s)));
				oldMIPanel.add(oldMIPanels.lastElement(),omiGBC);
			}
		}
		JScrollPane oldMIJSP = new JScrollPane(oldMIPanel);
		oldMIJSP.setPreferredSize(new Dimension(300,200));
		oldMIJSP.getVerticalScrollBar().setUnitIncrement(20);
		JPanel oldMIJSPPanel = new JPanel();
		oldMIJSPPanel.setBorder(BorderFactory.createTitledBorder(LANG.getMetainformation()));
		oldMIJSPPanel.add(oldMIJSP);
		
		JPanel oldValuesPanel = new JPanel();
		oldValuesPanel.setLayout(new BoxLayout(oldValuesPanel,BoxLayout.Y_AXIS));
		oldValuesPanel.setBackground(Color.lightGray);
		oldValuesPanel.setBorder(BorderFactory.createTitledBorder(LANG.getOldValues()));
		oldValuesPanel.add(oldPrefixPanel);
		oldValuesPanel.add(oldMIJSPPanel);
		
		newPreFixPanel = new JPanel();
		newPreFixPanel.setBorder(BorderFactory.createTitledBorder(LANG.getArticle()));
	
		newMIPanels = new Vector<MetaElement>();
		newMetaInformationPanel = new JPanel();
		newMetaInformationPanel.setLayout(new BoxLayout(newMetaInformationPanel,BoxLayout.Y_AXIS));
		
		JScrollPane initialMIJSP = new JScrollPane(newMetaInformationPanel);
		initialMIJSP.setPreferredSize(new Dimension(300,200));
		initialMIJSP.getVerticalScrollBar().setUnitIncrement(20);
		JPanel newMIJSPPanel = new JPanel();
		newMIJSPPanel.setBorder(BorderFactory.createTitledBorder(LANG.getMetainformation()));
		newMIJSPPanel.add(initialMIJSP);
		
		JPanel initialValuesPanel = new JPanel();
		initialValuesPanel.setBorder(BorderFactory.createTitledBorder(LANG.getInitialValues()));
		initialValuesPanel.setLayout(new BoxLayout(initialValuesPanel,BoxLayout.Y_AXIS));
		initialValuesPanel.setBackground(Color.lightGray);
		initialValuesPanel.add(newPreFixPanel);
		initialValuesPanel.add(newMIJSPPanel);
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(10,10,10,10);
		gbc.gridy = 0;
		this.add(title,gbc);
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		this.add(oldValuesPanel,gbc);
		
		gbc.gridy = 0;
		gbc.gridx = 1;
		this.add(tcJCB,gbc);
		gbc.gridy = 1;
		this.add(initialValuesPanel,gbc);
		this.setBackground(Color.darkGray);
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				setNewTCPanel();
				tcJCB.setSelectedItem(oldTCName);
			}
		});
	}
	
	private void setNewTCPanel(){
		newPreFixPanel.removeAll();
		newMetaInformationPanel.removeAll();
		newMIPanels.removeAllElements();
		TermClass tc = newTermClasses.get((String)tcJCB.getSelectedItem());
		if(tc.hasPrefix()){
			newPreFixME = new MetaElement(SET,tc.getPreFixName(),tc.getPreFixVal());
			newPreFixPanel.add(newPreFixME);
		}
		if(!tc.isEmpty()){
			int i=0;
			for(String s : tc.getSortedKeys()){
				newMIPanels.add(new MetaElement(SET,s,tc.getValues(s)));
				newMetaInformationPanel.add(newMIPanels.elementAt(i++));
			}
		}
		setOldTCPanels();
	}
	
	private void setOldTCPanels(){
		prefixConvertPanel.updateContent(newTermClasses.get((String)tcJCB.getSelectedItem()).hasPrefix(), newTermClasses.get((String)tcJCB.getSelectedItem()).getPreFixVal());
		if(!oldTC.isEmpty()){
			if(newTermClasses.get((String)tcJCB.getSelectedItem()).hasPrefix()){
				
			}
			for(ConvertMetaInformationPanel panel : oldMIPanels){
				panel.setEntirelyActivated(false);
			}
			if(!newTermClasses.get((String)tcJCB.getSelectedItem()).isEmpty()){
				int counter=0;
				for(ConvertMetaInformationPanel panel : oldMIPanels){
					panel.setActivated(true);
					panel.setComboBoxValues(newTermClasses.get((String)tcJCB.getSelectedItem()).getSortedKeys());
					panel.setComboBoxSelectedItemToOldMIName();
					if(panel.getKeepInformation()) counter++;
					else panel.setContentActivated(false);
				}
				if(oldMIPanels.size()>newMIPanels.size() && counter>=newMIPanels.size()){
					for(ConvertMetaInformationPanel p : oldMIPanels){
						if(!p.getKeepInformation())p.setActivated(false);
					}
					counter=0;
					for(ConvertMetaInformationPanel p : oldMIPanels){
						if(counter>=newMIPanels.size())p.setActivated(false);
						if(p.getKeepInformation()) counter++;
					}
				}
			}
		}
	}
	
	public TermClass getNewTermClass(){
		return newTermClasses.get((String)tcJCB.getSelectedItem());
	}
	
	public String getNewArticleInitialValue(){
		return newPreFixME.getValue();
	}
	
	public String getConvertedArticle(String old){
		return prefixConvertPanel.getConvertedArticle((String)tcJCB.getSelectedItem(),old);
	}
	
	public String getInitialValueOfMI(String nameArg){
		if(!newTermClasses.get((String)tcJCB.getSelectedItem()).isEmpty()){
			for(MetaElement me : newMIPanels){
				if(me.getMetaName().equals(nameArg)){
					return me.getValue();
				}
			}
		}
		return null;
	}
	
	public String getConvertedMetaInformation(String oldMIName, String oldMIValue){
		for(ConvertMetaInformationPanel cmip : oldMIPanels){
			if(cmip.getOldMIName().equals(oldMIName)){
				return cmip.getNewMIValueFor(oldMIValue);
			}
		}
		return "";
	}
	
	public String getNewMINameOf(String oldMIName){
		for(ConvertMetaInformationPanel cmip : oldMIPanels){
			if(cmip.getOldMIName().equals(oldMIName)){
				return cmip.getNewMINameSelection();
			}
		}
		return null;
	}
	
	public String getOldMINameOf(String newMIName){
		for(ConvertMetaInformationPanel cmip : oldMIPanels){
			if(cmip.getNewMINameSelection().equals(newMIName)){
				return cmip.getOldMIName();
			}
		}
		return null;
	}
	
	private class ConvertMetaInformationPanel extends JPanel{
		JComboBox coBo;
		String miName;
		Vector<String> values;
		JCheckBox enableCB;
		JPanel componentsPanel;
		HashMap<String,JComboBox> oldAndNewValuesJCBHM = new HashMap<String,JComboBox>();
		HashMap<String,TribuitTextField> oldAndNewValuesTFHM = new HashMap<String,TribuitTextField>();
		JCheckBox 	replaceEmptyValuesByInitialValueCB = new JCheckBox(LANG.getReplaceEmptyValuesByInitialValue()),
					copyNewValuesIntoComboBoxListCB = new JCheckBox(LANG.getCopyNewValuesIntoComboBox()) ;
		GridBagConstraints cpGBC;
		
		public ConvertMetaInformationPanel(String miName_Arg, Vector<String> values_Arg){
			super();
			miName = miName_Arg;
			values = values_Arg;
			cpGBC = new GridBagConstraints();
			cpGBC.anchor = GridBagConstraints.NORTHWEST;
			cpGBC.fill = GridBagConstraints.BOTH;
			StringBuffer titleContent = new StringBuffer();
			titleContent.append(miName + ((values==null)?" (text field)":" (combo box)"));
			coBo = new JComboBox();
			coBo.addItemListener(new ItemListener(){
				public void itemStateChanged(ItemEvent e){
					//make sure this value is not selectable in another combo box
					for(ConvertMetaInformationPanel p : oldMIPanels){
						if(p!=null && !p.getOldMIName().equals(miName)){
							if( p.getNewMINameSelection()!=null && p.getKeepInformation() && p.getNewMINameSelection().equals((String)coBo.getSelectedItem())){
								p.setNextComboBoxItem();
							}
						}
					}
					setNewValuesPanel();
				}
			});
			coBo.setEnabled(false);
			enableCB = new JCheckBox(LANG.getKeepInformationAs());
			enableCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					coBo.setEnabled(enableCB.isSelected());
					int counter=0;
					for(ConvertMetaInformationPanel p : oldMIPanels){
						p.setActivated(true);
						if(p.getKeepInformation()) counter++;
					}
					if(oldMIPanels.size()>newMIPanels.size() && counter>=newMIPanels.size()){
						for(ConvertMetaInformationPanel p : oldMIPanels){
							if(!p.getKeepInformation())p.setActivated(false);
						}
						counter=0;
						for(ConvertMetaInformationPanel p : oldMIPanels){
							if(counter>=newMIPanels.size())p.setActivated(false);
							if(p.getKeepInformation()) counter++;
						}
					}
					for(ConvertMetaInformationPanel p : oldMIPanels){
						if(p!=null && !p.getOldMIName().equals(miName)){
							if( p.getNewMINameSelection()!=null && p.getKeepInformation() && p.getNewMINameSelection().equals((String)coBo.getSelectedItem())){
								p.setNextComboBoxItem();
							}
						}
					}
					setContentActivated(enableCB.isSelected());
				}
			});
			componentsPanel = new JPanel();
			componentsPanel.setLayout(new GridBagLayout());
			this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),miName+((values==null)?" (text field)":" (combo box)")));
			this.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.NORTHWEST;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.weighty = 1;
			gbc.weightx = 1;
			gbc.insets = new Insets(5,5,5,0);
			this.add(enableCB,gbc);
			gbc.gridy = 1;
			this.add(coBo,gbc);
			gbc.gridy = 2;
			this.add(componentsPanel,gbc);
		}
		
		public void setNewValuesPanel(){
			componentsPanel.removeAll();
			oldAndNewValuesJCBHM = new HashMap<String,JComboBox>();
			oldAndNewValuesTFHM = new HashMap<String,TribuitTextField>();
			cpGBC.gridy = 0;
			cpGBC.gridx = 1;
			if(oldTermClasses.get(oldTCName).getValues(miName)==null){
				if(newTermClasses.get((String)tcJCB.getSelectedItem()).getValues((String)coBo.getSelectedItem()) == null){
					componentsPanel.add(replaceEmptyValuesByInitialValueCB,cpGBC);
				}
				else{
					componentsPanel.add(copyNewValuesIntoComboBoxListCB, cpGBC);
				}
			}
			else{
				if(newTermClasses.get((String)tcJCB.getSelectedItem()).getValues((String)coBo.getSelectedItem()) == null){
					for(String s : oldTermClasses.get(oldTCName).getValues(miName)){
						oldAndNewValuesTFHM.put(s,new TribuitTextField(SET,10));
						cpGBC.gridx = 1;
						componentsPanel.add(new JLabel(s),cpGBC);
						cpGBC.gridx = 2;
						componentsPanel.add(new JLabel("->"),cpGBC);
						cpGBC.gridx = 3;
						componentsPanel.add(oldAndNewValuesTFHM.get(s),cpGBC);
						oldAndNewValuesTFHM.get(s).setText(s);
						++cpGBC.gridy;
					}
				}
				else{
					cpGBC.gridy = 0;
					for(String s : oldTermClasses.get(oldTCName).getValues(miName)){
						oldAndNewValuesJCBHM.put(s,new JComboBox(newTermClasses.get((String)tcJCB.getSelectedItem()).getValues((String)coBo.getSelectedItem())));
						cpGBC.gridx = 1;
						componentsPanel.add(new JLabel(s),cpGBC);
						cpGBC.gridx = 2;
						componentsPanel.add(new JLabel("->"),cpGBC);
						cpGBC.gridx = 3;
						componentsPanel.add(oldAndNewValuesJCBHM.get(s),cpGBC);
						oldAndNewValuesJCBHM.get(s).setSelectedItem(s);
						++cpGBC.gridy;
					}
				}
			}
			frame.pack();
		}
		
		public boolean isSelectable(){
			return enableCB.isEnabled();
		}
		
		public String getOldMIName(){
			return miName;
		}
		
		public String getNewMINameSelection(){
			return (String)coBo.getSelectedItem();
		}
		
		public void setComboBoxSelectedItemToOldMIName(){
			coBo.setSelectedItem(miName);
		}
		
		public void setNextComboBoxItem(){
			if(coBo!=null && coBo.getItemCount()>0){
				if(coBo.getSelectedIndex()==coBo.getItemCount()-1){
					coBo.setSelectedIndex(coBo.getSelectedIndex()-1);
				}
				else coBo.setSelectedIndex(coBo.getSelectedIndex()+1);
			}
		}
		
		public void setComboBoxValues(Vector<String> arg){
			coBo.removeAllItems();
			if(arg!=null && arg.size()>0){
				for(String s : arg){
					if(s!=null)coBo.addItem(s);
				}
			}
			else coBo.setEnabled(false);
		}
		
		public void setEntirelyActivated(boolean arg){
			setActivated(arg);
			setContentActivated(arg);
			
		}
		
		public void setActivated(boolean arg){
			enableCB.setEnabled(arg);
			coBo.setEnabled(arg?enableCB.isSelected():arg);
		}
		
		public void setContentActivated(boolean arg){
			if(oldAndNewValuesJCBHM!=null && !oldAndNewValuesJCBHM.isEmpty()){
				for(String s : oldAndNewValuesJCBHM.keySet()){
					oldAndNewValuesJCBHM.get(s).setEnabled(arg);
				}
			}
			if(oldAndNewValuesTFHM!=null && !oldAndNewValuesTFHM.isEmpty()){
				for(String s : oldAndNewValuesTFHM.keySet()){
					oldAndNewValuesTFHM.get(s).setEnabled(arg);
				}
			}
			if(replaceEmptyValuesByInitialValueCB != null){
				replaceEmptyValuesByInitialValueCB.setEnabled(arg);
			}
			if(copyNewValuesIntoComboBoxListCB != null){
				copyNewValuesIntoComboBoxListCB.setEnabled(arg);
			}
		}
		
		public boolean getKeepInformation(){
			return enableCB.isEnabled()&&enableCB.isSelected();
		}
		
		public String getNewMIValueFor(String oldMIValueArg){
			TermClass tc = newTermClasses.get((String)tcJCB.getSelectedItem());
			if(!tc.isEmpty()&&!oldMIPanels.isEmpty()){
				if(getKeepInformation()){
					if(oldTermClasses.get(oldTCName).getValues(miName)==null){
						if(tc.getValues((String)coBo.getSelectedItem()) == null){
							if(oldMIValueArg.equals("")){
								if(replaceEmptyValuesByInitialValueCB.isSelected()) return getInitialValueOfMI((String)coBo.getSelectedItem());
							}
							return oldMIValueArg;
						}
						else{
							if(copyNewValuesIntoComboBoxListCB.isSelected()){
								//FIXME CHECK ME
								if(!tc.getValues((String)coBo.getSelectedItem()).contains(oldMIValueArg)){
									tc.getValues((String)coBo.getSelectedItem()).add(oldMIValueArg);
									return oldMIValueArg;
								}
							}
							return getInitialValueOfMI((String)coBo.getSelectedItem());
						}
					}
					else{
						if(tc.getValues((String)coBo.getSelectedItem()) == null){
								oldAndNewValuesTFHM.get(oldMIValueArg).getText();
						}
						else{
							return (String)oldAndNewValuesJCBHM.get(oldMIValueArg).getSelectedItem();
						}
					}
				}
				else return getInitialValueOfMI((String)coBo.getSelectedItem());
			}
			return null;
		}
	}

	private class ConvertPrefixPanel extends JPanel{
		private String pfName;
		private Vector<String> pfValues;
		private JCheckBox keepInformationCB,replaceEmptyValuesByInitialValueCB,addNewValuesToComboBoxCB;
		private JPanel componentsPanel;
		private GridBagConstraints cpGBC;
		private HashMap<String,TribuitTextField> oldAndNewValuesTFHM = new HashMap<String,TribuitTextField>();
		private HashMap<String,JComboBox> oldAndNewValuesJCBHM = new HashMap<String,JComboBox>();
		
		public ConvertPrefixPanel(String name_arg,Vector<String> values_arg){
			super();
			pfName = name_arg;
			pfValues = values_arg;
			
			keepInformationCB = new JCheckBox(LANG.getKeepInformation());
			keepInformationCB.addActionListener(new KeepInfoCBListener());
			
			replaceEmptyValuesByInitialValueCB = new JCheckBox(LANG.getReplaceEmptyValuesByInitialValue());
			addNewValuesToComboBoxCB = new JCheckBox(LANG.getCopyNewValuesIntoComboBox());
			
			componentsPanel = new JPanel();
			componentsPanel.setLayout(new GridBagLayout());
			cpGBC = new GridBagConstraints();
			JScrollPane jsp = new JScrollPane(componentsPanel);
			jsp.getVerticalScrollBar().setUnitIncrement(30);
			jsp.setPreferredSize(new Dimension(300,70));
			
			this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
			this.add(keepInformationCB);
			this.add(jsp);
			
		}
		
		public void updateContent(boolean hasArticle, Vector<String> vals){
			keepInformationCB.setEnabled(hasArticle);
			updateComponentsPanel(hasArticle,vals);
			activateContent(hasArticle?keepInformationCB.isSelected():false);
		}
		
		public void updateComponentsPanel(boolean hasArticle,Vector<String> vals){
			componentsPanel.removeAll();
			cpGBC.gridy = 0;
			cpGBC.gridx = 0;
			if(hasArticle){
				if(pfValues == null){
					if(vals == null){
						componentsPanel.add(replaceEmptyValuesByInitialValueCB,cpGBC);
					}
					else{
						componentsPanel.add(addNewValuesToComboBoxCB,cpGBC);
					}
				}
				else{
					if(vals == null){
						oldAndNewValuesTFHM = new HashMap<String,TribuitTextField>();
						for(String s : pfValues){
							oldAndNewValuesTFHM.put(s,new TribuitTextField(SET,10));
							cpGBC.gridx = 1;
							componentsPanel.add(new JLabel(s),cpGBC);
							cpGBC.gridx = 2;
							componentsPanel.add(new JLabel("->"),cpGBC);
							cpGBC.gridx = 3;
							componentsPanel.add(oldAndNewValuesTFHM.get(s),cpGBC);
							oldAndNewValuesTFHM.get(s).setText(s);
							++cpGBC.gridy;
						}
					}
					else{
						oldAndNewValuesJCBHM = new HashMap<String,JComboBox>();
						for(String s : pfValues){
							oldAndNewValuesJCBHM.put(s,new JComboBox(vals));
							cpGBC.gridx = 1;
							componentsPanel.add(new JLabel(s),cpGBC);
							cpGBC.gridx = 2;
							componentsPanel.add(new JLabel("->"),cpGBC);
							cpGBC.gridx = 3;
							componentsPanel.add(oldAndNewValuesJCBHM.get(s),cpGBC);
							oldAndNewValuesJCBHM.get(s).setSelectedItem(s);
							++cpGBC.gridy;
						}
					}
				}
			}
		}
		
		
		
		public void activateContent(boolean arg){
			addNewValuesToComboBoxCB.setEnabled(arg);
			replaceEmptyValuesByInitialValueCB.setEnabled(arg);
			if(!oldAndNewValuesTFHM.isEmpty()){
				for(String s : oldAndNewValuesTFHM.keySet().toArray(new String[]{})){
					oldAndNewValuesTFHM.get(s).setEnabled(arg);
				}
			}
			if(!oldAndNewValuesJCBHM.isEmpty()){
				for(String s : oldAndNewValuesJCBHM.keySet().toArray(new String[]{})){
					oldAndNewValuesJCBHM.get(s).setEnabled(arg);
				}
			}
		}
		
		private class KeepInfoCBListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				activateContent(keepInformationCB.isSelected());
			}
		}
		
		public boolean getKeepArticleInformation(){
			return keepInformationCB.isSelected();
		}
		
		/**
		 * Returns the new value assigned to the old one.
		 * 
		 * @param tc currently selected term class
		 * @param old old prefix value
		 * @return
		 */
		public String getConvertedArticle(String tc, String old){
			if(newTermClasses.get(tc).hasPrefix()){
				if(keepInformationCB.isEnabled() && keepInformationCB.isSelected()){
					if(pfValues==null){
						if(newTermClasses.get(tc).getPreFixVal()==null){
							if(old.equals("")) return (replaceEmptyValuesByInitialValueCB.isSelected())?getNewArticleInitialValue():"";
							else return old;
						}
						else{
							if(addNewValuesToComboBoxCB.isSelected()){
								if(!getNewTermClass().getPreFixVal().contains(old) && !old.equals("")){
									getNewTermClass().addPreFixValue(old);
								}
							}
							return old;
						}
					}
					else{
						if(newTermClasses.get(tc).getPreFixVal()==null){
							return oldAndNewValuesTFHM.get(old).getText();
						}
						else{
							return (String)oldAndNewValuesJCBHM.get(old).getSelectedItem();
						}
					}
				}
				else return getNewArticleInitialValue();
			}
			return null;
		}
	}
}
